package com.lg.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 分页控制标签 
 * @author 
 * @see /WEB-INF/pageUtil.tld
 */
public class PageUtil extends TagSupport {
	private String formAction;// form名称

	private int onePageCount; // 每页显示的记录数

	private int resultCount; //  总共有的记录数

	private int currentPage; //  当前页
	
	private String isShowInfo;

	public PageUtil() {
	}

	public void setIsShowInfo(String isShowInfo) {
		this.isShowInfo = isShowInfo.trim();
	}

	public void setFormAction(String formAction) {
		this.formAction = formAction.trim();
	}

	public void setCurrentPage(String currentPage) {
		if (currentPage == null || "".equals(currentPage)|| "null".equals(currentPage) ) {
			currentPage = "0";
		}
		this.currentPage = Integer.parseInt(currentPage);

	}

	public void setOnePageCount(String onePageCount) {
		if (onePageCount == null || "".equals(onePageCount)|| "null".equals(onePageCount)  ) {
			onePageCount = "10";
		}
		this.onePageCount = Integer.parseInt(onePageCount);

	}

	public void setResultCount(String resultCount) {
		if (resultCount == null || "".equals(resultCount) || "null".equals(resultCount) ) {
			resultCount = "0";
		}
		this.resultCount = Integer.parseInt(resultCount);

	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) this.pageContext
				.getRequest();
		String rootPath = request.getContextPath();

		int pageCount = 0;


		// TODO 每页分页数量
		if (resultCount % onePageCount == 0) {
			pageCount = resultCount / onePageCount;
		} else {
			pageCount = resultCount / onePageCount + 1;
		}
		try {
			// TODO 这里面的onsubmit没有用处，注释掉
			out.println("<form id=\"pageFormCL\" name=\"pageFormCL\" action=\"\" method=\"post\" onSubmit=\"return false;\">");
			out.println("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"  >");
			int tempI = 0;
			Enumeration er = request.getParameterNames();
			String tempParaName = "", tempParaValue = "", tempTransStr = "&";
			while (er.hasMoreElements()) {
				tempI++;
				tempParaName = (String) er.nextElement();
				tempParaValue = request.getParameter(tempParaName);
				//pageNum如果生成hidden,会与下面的text重复, 取值时undefine
				if (!tempParaName.matches(".")&&!"pageNum".equals(tempParaName)) {
//					tempTransStr = tempTransStr + tempParaName + "="
//							+ tempParaValue + "&";

					out.println("<input type=\"hidden\" name=\""+tempParaName+"\" value=\""+tempParaValue+"\" />");
				}
				
			}
			tempTransStr = tempTransStr.substring(0,
					(tempTransStr.length() - 1));
			out.println("<TR class='font12'><TD align=\"right\">");

			if (currentPage <= 1) {
				out.println("首页&nbsp;&nbsp;");
				out.println("上一页&nbsp;&nbsp;");
				currentPage = 1;
			} else {
				// TODO ��һҳ ��һҳ �ĳ�onsubmit()
//				out.println("<A HREF=" + rootPath + "/" + formAction
//						+ "&page=1&onePageCount=" + onePageCount
//						+tempTransStr+ ">��ҳ</A>&nbsp;&nbsp;");
//				out.println("<A HREF=" + rootPath + "/" + formAction + "&page="
//						+ (currentPage - 1) + "&onePageCount=" + onePageCount
//						+tempTransStr+ ">��һҳ</A>&nbsp;&nbsp; ");
				out.println("<A HREF=\"javascript:submitPageform("+1+","+onePageCount+")\">首页</A>&nbsp;&nbsp;");
				out.println("<A HREF=\"javascript:submitPageform("+(currentPage - 1)+","+onePageCount+")\">上一页</A>&nbsp;&nbsp; ");
			}

			if (currentPage >= pageCount) {
				out.println("下一页&nbsp;&nbsp;");
				out.println("尾页&nbsp;&nbsp;");
				currentPage = pageCount;
			} else {
//				out.println("<A HREF=" + rootPath + "/" + formAction + "&page="
//						+ (currentPage + 1) + "&onePageCount=" + onePageCount
//						+tempTransStr+ ">��һҳ</A>&nbsp;&nbsp; ");
//				out.println("<A HREF=" + rootPath + "/" + formAction + "&page="
//						+ pageCount + "&onePageCount=" + onePageCount
//						+tempTransStr+ ">βҳ</A>&nbsp;&nbsp;");
				
				out.println("<A HREF=\"javascript:submitPageform("+(currentPage + 1)+","+onePageCount+")\">下一页</A>&nbsp;&nbsp;");
				out.println("<A HREF=\"javascript:submitPageform("+pageCount+","+onePageCount+")\">尾页</A>&nbsp;&nbsp; ");
			}
			out.println(" 第" + currentPage + "页");
			out.println(" 共" + pageCount + "页");
			out.println(" 共" + resultCount + "条");
			//�������ת���ڼ�ҳ add by tangwei
			if(request.getParameter("pageNum")==null||"".equals(request.getParameter("pageNum"))){
				String str = "";
				out.println("&nbsp;转至第&nbsp;<input type=\"text\" name=\"pageNum\" value=\""+str+"\" size=\"2\"  style=\"height:13px\"   >&nbsp;ҳ");
			}else{
				int str = Integer.valueOf(request.getParameter("pageNum"));
				if(str>pageCount)
					str = pageCount;
				out.println("&nbsp;转至第&nbsp;<input type=\"text\" name=\"pageNum\" value=\""+str+"\" size=\"2\"  style=\"height:13px\"   >&nbsp;ҳ");
			}
			//			out.println("<input type=\"button\" value=\"go\" onclick=\"javascript:var pageNum1=document.pageFormCL.pageNum.value;document.pageFormCL.action='" + rootPath + "/" + formAction + "&onePageCount=" + onePageCount +"&page='" + " + pageNum1 + '" + tempTransStr + "';" + "document.pageFormCL.submit();\">");
			out.println("<td>&nbsp;");
			out.println("<input type=\"image\" src=\"" + request.getContextPath() + "/images/button14_03.gif\" onclick=\"javascript: submitPageform(document.pageFormCL.pageNum.value,"+onePageCount+")\">");
			out.println("</td>");
			// tangwei add end
			out.println("</TD>");
			out.println("</TR>");
			out.println("<input type=\"hidden\" name=\"onePageCount\" value=\""+this.onePageCount+"\">");
			out.println("<input type=\"hidden\" name=\"page\" value=\""+this.currentPage+"\">");
			out.println("</table></form>");
			
			out.println("<script type=\"text/javascript\">");
			out.println("function submitPageform(page,onePageCount){");
			
			//out.println(" var r = /^[0-9]*[1-9][0-9]*$/ ; if(!r.test(page)){alert(\"您输入的页数非正整数!\");document.pageFormCL.pageNum.value=\"\";return;};");
			//修改当输入非正整数默认显示第一页
			out.println(" var r = /^[0-9]*[1-9][0-9]*$/ ; if(!r.test(page)){page=1;document.pageFormCL.pageNum.value=\"1\";};");
			//out.println("if(page>"+pageCount+") { alert(\"大于最大页面\");return false; }");
			out.println("if(page<1) {page=1;}");
			out.println("document.pageFormCL.action=\"" + rootPath + "/" + formAction + "&page=\"+page+\"&onePageCount=\"+onePageCount+\"\"");
			out.println("document.pageFormCL.submit();");
			out.println("}");
			out.println("</script>");
			
			out.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new JspException(ex.getMessage());
		}
		
		//����̨���. ��ǰ���ͬ��
//		PrintWriter out2=new PrintWriter(//System.out);
//		if (resultCount % onePageCount == 0) {
//			pageCount = resultCount / onePageCount;
//		} else {
//			pageCount = resultCount / onePageCount + 1;
//		}
//		try {
//			// TODO �������onsubmitû���ô���ע�͵�
//			out2.println("<form name=\"pageFormCL\" action=\"" 
//					+ this.formAction + "\" method=\"post\" >");
//			out2.println("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\" >");
//			int tempI = 0;
//			Enumeration er = request.getParameterNames();
//			String tempParaName = "", tempParaValue = "", tempTransStr = "&";
//			while (er.hasMoreElements()) {
//				tempI++;
//				tempParaName = (String) er.nextElement();
//				tempParaValue = request.getParameter(tempParaName);
//				//pageNum������hidden,���������text�ظ�, ȡֵʱundefine
//				if (!tempParaName.matches(".")&&!"pageNum".equals(tempParaName)) {
////					tempTransStr = tempTransStr + tempParaName + "="
////							+ tempParaValue + "&";
//
//					out2.println("<input type=\"hidden\" name=\""+tempParaName+"\" value=\""+tempParaValue+"\" />");
//				}
//				
//			}
//			tempTransStr = tempTransStr.substring(0,
//					(tempTransStr.length() - 1));
//			out2.println("<TR><TD align=\"right\">");
//
//			if (currentPage <= 1) {
//				out2.println("��ҳ&nbsp;&nbsp;");
//				out2.println("��һҳ&nbsp;&nbsp; ");
//				currentPage = 1;
//			} else {
//				// TODO ��һҳ ��һҳ �ĳ�onsubmit()
////				out2.println("<A HREF=" + rootPath + "/" + formAction
////						+ "&page=1&onePageCount=" + onePageCount
////						+tempTransStr+ ">��ҳ</A>&nbsp;&nbsp;");
////				out2.println("<A HREF=" + rootPath + "/" + formAction + "&page="
////						+ (currentPage - 1) + "&onePageCount=" + onePageCount
////						+tempTransStr+ ">��һҳ</A>&nbsp;&nbsp; ");
//				out2.println("<A HREF=\"javascript:submitPageform("+1+","+onePageCount+")\">��ҳ</A>&nbsp;&nbsp;");
//				out2.println("<A HREF=\"javascript:submitPageform("+(currentPage - 1)+","+onePageCount+")\">��һҳ</A>&nbsp;&nbsp; ");
//			}
//
//			if (currentPage >= pageCount) {
//				out2.println("��һҳ&nbsp;&nbsp;");
//				out2.println("βҳ&nbsp;&nbsp;");
//				currentPage = pageCount;
//			} else {
////				out2.println("<A HREF=" + rootPath + "/" + formAction + "&page="
////						+ (currentPage + 1) + "&onePageCount=" + onePageCount
////						+tempTransStr+ ">��һҳ</A>&nbsp;&nbsp; ");
////				out2.println("<A HREF=" + rootPath + "/" + formAction + "&page="
////						+ pageCount + "&onePageCount=" + onePageCount
////						+tempTransStr+ ">βҳ</A>&nbsp;&nbsp;");
//				
//				out2.println("<A HREF=\"javascript:submitPageform("+(currentPage + 1)+","+onePageCount+")\">��һҳ</A>&nbsp;&nbsp;");
//				out2.println("<A HREF=\"javascript:submitPageform("+pageCount+","+onePageCount+")\">βҳ</A>&nbsp;&nbsp; ");
//			}
//			out2.println(" ��" + currentPage + "ҳ");
//			out2.println(" ��" + pageCount + "ҳ");
//			out2.println(" ��" + resultCount + "��");
//			//�������ת���ڼ�ҳ add by tangwei
//			out2.println("&nbsp;ת����<input type=\"text\" name=\"pageNum\" value=\""+(request.getParameter("pageNum")==null?"":(request.getParameter("pageNum")).toString())+"\" size=\"3\">ҳ");
////			out2.println("<input type=\"button\" value=\"go\" onclick=\"javascript:var pageNum1=document.pageFormCL.pageNum.value;document.pageFormCL.action='" + rootPath + "/" + formAction + "&onePageCount=" + onePageCount +"&page='" + " + pageNum1 + '" + tempTransStr + "';" + "document.pageFormCL.submit();\">");
//			
//			out2.println("<input type=\"button\" value=\"go\" onclick=\"javascript: alert(document.pageFormCL.pageNum.value);\">");
//			out2.println("<input type=\"button\" value=\"go\" onclick=\"javascript: submitPageform(document.pageFormCL.pageNum.value,"+onePageCount+")\">");
//			// tangwei add end
//			out2.println("</TD>");
//			out2.println("</TR>");
//			out2.println("<input type=\"hidden\" name=\"onePageCount\">");
//			out2.println("<input type=\"hidden\" name=\"page\">");
//			out2.println("</table></form>");
//			
//			out2.println("<script type=\"text/javascript\">");
//			out2.println("function submitPageform(page,onePageCount){");
//			out2.println("pageFormCL.action=\"" + rootPath + "/" + formAction + "&page=\"+page+\"&onePageCount=\"+onePageCount+\"\"");
//			out2.println("pageFormCL.submit()");
//			out2.println("}");
//			out2.println("</script>");
//			
//			out2.flush();
//			//System.out.println("end");
//		} catch (Exception ex) {
//			throw new JspException(ex.getMessage());
//		}
		return Tag.SKIP_BODY;
	}
}